package
{
	/**
	 * This class is the Document class in the Flash
	 * document, and only deals with sending data to the 
	 * text fields. 
	 */	
	import classes.mvc.UIModel;
	
	import data.LoadDummyData;
	import data.LoadTweetService;
	
	import events.Static_EventDispatcher;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import mvc.UIView;
	
	import ui.graphs.Graph;
	import ui.twitter.TweetINIT;
	
	import utils.StringUtils;

	
	public class UI extends MovieClip
	{
		private var _testMC 		: MovieClip;
		private var _ld				: LoadDummyData;
		
		private var _xmlArr1		: Array;
		private var _xmlArr2		: Array;
		
		private var _db				: Number = 0;
		private var _motion			: String = "Low";
		private var _p				: Number = 0;
		
		private var _tf_audio		: TextField;
		private var _tf_motion		: TextField;
		private var _tf_people		: TextField;
		private var _tf_twitter		: TextField;
		
		private var _jsonData		: Object;
		
		private var _flowerNum		: Number = 0;
		private var _fruitNum		: Number = 0;
		
		private var _servTest		: LoadTweetService = new LoadTweetService();
		
		/**
		 * Entry point that has Static Event Dispatchers
		 * set up to listen to the 3 graphs, and the Twitter
		 * feed.
		 * 
		 * We are also setting the frame rate here, and bringing
		 * in dummyData.xml again to parse the XML in this class. This 
		 * is a bit redundant as it is being parsed elsewhere, but I was crunched
		 * for time and didn't make a model class to handle all this. May want
		 * to do that.
		 */		
		public function UI()
		{
			stage.frameRate = 24;
			
			_testMC = getChildByName("test_mc") as MovieClip;
			
			Static_EventDispatcher.addEventListener(Graph.AUDIO_CHANGE, _audioTxtChange);
			Static_EventDispatcher.addEventListener(Graph.MOTION_CHANGE_LOW, _motionTxtChange);
			Static_EventDispatcher.addEventListener(Graph.MOTION_CHANGE_MOD, _motionTxtChange);
			Static_EventDispatcher.addEventListener(Graph.PEOPLE_ADD_MINUS, _peopleAddMinus);
			Static_EventDispatcher.addEventListener(TweetINIT.JSON_DATA_LOADED, _testing);
			
			_ld = new LoadDummyData();
			_ld.addEventListener(LoadDummyData.XML_LOADED, _parseXML);
			_ld.init("../xml/dummyData.xml");
			
			_tf_audio = getChildByName("audio_txt") as TextField;
			_tf_motion = getChildByName("motion_txt") as TextField;
			_tf_people = getChildByName("people_txt") as TextField;
			_tf_twitter = getChildByName("twitter_txt") as TextField;
		}
		
		/**
		 * Going out and grabbing JSON data again. As above, this 
		 * is being grabbed else where, so a global model to handle
		 * this is definitely required.
		 * 
		 * @param $e : Event
		 */		
		private function _testing($e:Event) : void
		{
			_servTest.init("http://10.3.60.173/services/GetTweets?query=%40united&count="); // 41
			_servTest.addEventListener(LoadTweetService.JSON_LOADED, _onLoad);
		}
		
		/**
		 * Parsing out the JSON data again.
		 * 
		 * @param $e : Event
		 */		
		private function _onLoad($e:Event) : void
		{
			_jsonData = new Object();
			_jsonData = _servTest.data;
			
			for (var m : Number = 0; m < _jsonData.length; m++)
				(_jsonData[m].Type == "B") ? _flowerNum++ : _fruitNum++;
			
			_tf_twitter.text = _flowerNum + " Blooms " + _fruitNum + " Fruit";
		}
		
		/**
		 * Parsing out the XML data.
		 * 
		 * @param $e : Event
		 */		
		private function _parseXML($e : Event) : void
		{
			_xmlArr1 = new Array();
			var tempStr : String = _ld.data..audio.@points;
			for (var i : Number = 0; i < 70; i++)
			{
				var numStr : String = StringUtils.beforeFirst(tempStr, ",");
				_xmlArr1.push(numStr);
				
				if (i < 70)
					tempStr = StringUtils.afterFirst(tempStr, numStr + ",");
				else
				{
					_xmlArr1.pop();
					_xmlArr1.push(tempStr);
				}
			}
			
			// --- Parsing out XML data again. Could probably do 
			// a bit of Generic programming to make this code less
			// redundant.
			
			_xmlArr2 = new Array();
			tempStr = _ld.data..people.@points;
			for (var j : Number = 0; j < 60; j++)
			{
				numStr = StringUtils.beforeFirst(tempStr, ",");
				_xmlArr2.push(numStr);
				
				if (j < 60)
					tempStr = StringUtils.afterFirst(tempStr, numStr + ",");
				else
				{
					_xmlArr2.pop();
					_xmlArr2.push(tempStr);
				}
			}
		}
		
		/**
		 * Parsing out XML data for Audio text field.
		 * 
		 * @param $e : Event
		 */		
		private function _audioTxtChange($e:Event) : void
		{
			// Doesn't seem to be working atm...
			if (_xmlArr1[_db] == undefined)
				_tf_audio.text = "0 dB";
			
			_tf_audio.text = _xmlArr1[_db++] + " dB";
		}
		
		/**
		 * Parsing out XML data for Motion text field.
		 * 
		 * @param $e : Event
		 */		
		private function _motionTxtChange($e:Event) : void
		{
			switch ($e.type)
			{
				case "Motion.Change.Low": _tf_motion.text = "Low"; break;
				case "Motion.Change.Mod": _tf_motion.text = "Moderate";	
			}
		}
		
		/**
		 * Parsing out XML data for People text field.
		 * 
		 * @param $e : Event
		 */		
		private function _peopleAddMinus($e:Event) : void
		{
			if (_xmlArr2[_p + 1] != _xmlArr2.length)
				_tf_people.text = _xmlArr2[_p++];
		}
	}
}
